package com.flying.scorer;

import com.flying.common.CommonApplication;
import com.flying.refdata.DataImporter;
import com.flying.scorer.model.equity.EquityScore;
import com.flying.scorer.repository.equity.EquityScoreRepository;
import com.flying.utils.excel.ExcelUtil;
import com.flying.utils.uniquekey.IDUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.FileOutputStream;
import java.util.List;


@SpringBootApplication
@ComponentScan(
        basePackageClasses = {
                CommonApplication.class,
                DataImporter.class,
                ScoreExporter.class},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.flying.scorer.ScoreRunner")})
public class ScoreExporter implements CommandLineRunner {
    private static Logger logger = LoggerFactory.getLogger(ScoreExporter.class);
    @Autowired
    private EquityScoreRepository repository;

    @Override
    public void run(String... args) throws Exception {
        long start = System.currentTimeMillis();
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("EquityScores");
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("证券代码");
        header.createCell(1).setCellValue("证券简称");
        header.createCell(2).setCellValue("评分日期");

        Pageable pageable = new PageRequest(0, 1000, new Sort("equityInfo.id", "date"));
        List<EquityScore> scores;
        int rowNum = 0;
        int colNum = 0;
        Row row = null;
        String colHeader;
        String currentCode = "";
        int currentDate = 0;
        do {
            scores = repository.findBySucceedTrue(pageable);
            pageable = pageable.next();
            for (EquityScore score : scores) {
                colHeader = score.getScorerName();
                colNum = ExcelUtil.indexOf(header, colHeader);
                if (colNum < 0) {
                    colNum = header.getLastCellNum();
                    header.createCell(colNum).setCellValue(colHeader);
                }
                if (currentDate != score.getDate() || !currentCode.equalsIgnoreCase(score.getEquityInfo().getCode())) {
                    rowNum++;
                    row = sheet.createRow(rowNum);
                    currentDate = score.getDate();
                    currentCode = score.getEquityInfo().getCode();
                }
                row.createCell(0).setCellValue(score.getEquityInfo().getCode());
                row.createCell(1).setCellValue(score.getEquityInfo().getName());
                row.createCell(2).setCellValue(score.getDate());
                row.createCell(colNum).setCellValue(score.getScore());
            }
        } while (scores.size() > 0);

        FileOutputStream fileOut = new FileOutputStream(IDUtil.getUUID() + ".xlsx");
        wb.write(fileOut);
        fileOut.close();
        wb.close();
        logger.warn("ScoreExporter" + (System.currentTimeMillis() - start));
    }

    public static void main(String[] args) {
        SpringApplication.run(ScoreExporter.class, args);
    }
}


